<?php
require_once("./includes/config.inc.php");
$examId = $_REQUEST['exam'];

function shuffle_array($list) { 
  if (!is_array($list)) return $list; 

  $keys = array_keys($list); 
  shuffle($keys); 
  $random = array(); 
  foreach ($keys as $key) { 
    $random[$key] = $list[$key]; 
  }
  return $random; 
} 





if(!$_SESSION['arrExamQuestions']){
	$sqlE = "select * from tbl_exam where id = '$examId' ";
	$resE = executeQuery($sqlE);
	if($rowE = mysql_fetch_array($resE)){
		$subjectId = $rowE['subjectId'];
		$noOfQuestion = $rowE['noOfQuestion'];
		$_SESSION['sessExamTime'] = $rowE['time'];
	}
	
	$sqlQ = " select * from tbl_questions where subjectId = '$subjectId' ";  
	$sqlQ.=" order by id desc ";
	$resQ = executeQuery($sqlQ);
	while($rowQ = mysql_fetch_array($resQ)){
		$arrExamQuestions[$rowQ['id']][] = $rowQ['question'];
		$sqlA = "select * from tbl_answers where questionId = '$rowQ[id]' ";
		$resA = executeQuery($sqlA);
		while($rowA = mysql_fetch_array($resA)){
			$arrExamAnswers[$rowQ[id]][$rowA['id']] = $rowA['answer'];
			$arrExamQuestions[$rowQ['id']][] = array($rowA['id'],$rowA['answer']);
			if($rowA['isRight']){
				$arrExamCorrectAnswer[$rowQ[id]] = $rowA['id'];
				$rightAnswerId = $rowA['id'];
			}
		}
		$arrExamQuestions[$rowQ['id']][] = $rightAnswerId;
	}
	
	$tmpArray = shuffle_array($arrExamQuestions,true);
	$_SESSION['arrExamQuestions'] = $tmpArray;
	$_SESSION['arrDisplayedQuestions'] =  array();
	$_SESSION['next'] = true;
}
//echo '<pre>';
//echo '<br> ========== <br> All Questions <br> ========== <br>';
//print_r($_SESSION['arrExamQuestions']);
?>